﻿// JScript File



function getDateFromFormat(val) {
    format = "d/M/yyyy";
    val = val + "";
    format = format + "";
    var i_val = 0;
    var i_format = 0;
    var c = "";
    var token = "";
    var token2 = "";
    var x, y;
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1;
    var date = 1;
    var hh = now.getHours();
    var mm = now.getMinutes();
    var ss = now.getSeconds();
    var ampm = "";

    while (i_format < format.length) {
        // Get next token from format string
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        // Extract contents of value based on format token
        if (token == "yyyy" || token == "yy" || token == "y") {
            if (token == "yyyy") { x = 4; y = 4; }
            if (token == "yy") { x = 2; y = 2; }
            if (token == "y") { x = 2; y = 4; }
            year = _getInt(val, i_val, x, y);
            if (year == null) { return 0; }
            i_val += year.length;
            if (year.length == 2) {
                if (year > 70) { year = 1900 + (year - 0); }
                else { year = 2000 + (year - 0); }
            }
        }
        else if (token == "MM" || token == "M") {
            month = _getInt(val, i_val, token.length, 2);
            if (month == null || (month < 1) || (month > 12)) { return 0; }
            i_val += month.length;
        }
        else if (token == "dd" || token == "d") {
            date = _getInt(val, i_val, token.length, 2);
            if (date == null || (date < 1) || (date > 31)) { return 0; }
            i_val += date.length;
        }
        else {
            if (val.substring(i_val, i_val + token.length) != token) { return 0; }
            else { i_val += token.length; }
        }
    }
    // If there are any trailing characters left in the value, it doesn't match
    if (i_val != val.length) { return 0; }
    // Is date valid for month?
    if (month == 2) {
        // Check for leap year
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { // leap year
            if (date > 29) { return 0; }
        }
        else { if (date > 28) { return 0; } }
    }
    if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
        if (date > 30) { return 0; }
    }
    // Correct hours value
    if (hh < 12 && ampm == "PM") { hh = hh - 0 + 12; }
    else if (hh > 11 && ampm == "AM") { hh -= 12; }
    var newdate = new Date(year, month - 1, date, hh, mm, ss);
    return newdate.getTime();
}

function _getInt(str, i, minlength, maxlength) {
    for (var x = maxlength; x >= minlength; x--) {
        var token = str.substring(i, i + x);
        if (token.length < minlength) { return null; }
        if (_isInteger(token)) { return token; }
    }
    return null;
}

function _isInteger(val) {
    var digits = "1234567890";
    for (var i = 0; i < val.length; i++) {
        if (digits.indexOf(val.charAt(i)) == -1) { return false; }
    }
    return true;
}

function isDate(datetimeValue) {
    var date = getDateFromFormat(datetimeValue);
    if (date == 0) {    //Date format is invalid
        return false;
    }
    return true;
}

function CompareCurrentDate(dateValue) {
    var curDate = new Date();
    var dateSplit = dateValue.split("/");
    var curOnlyDate = new Date(curDate.getYear(), curDate.getMonth(), curDate.getUTCDate());   //getUTCDate   return day in one month(1-31).  getDay()  return the day in one week(0-6)
    var inpDate = new Date(dateSplit[2], dateSplit[1] - 1, dateSplit[0]);
    if (inpDate < curOnlyDate)
        return false;
    else
        return true;
}

function EqualCurrentDate(dateValue) {
    var curDateFormat = getdate();    //dd/MM/yyyy format
    if (curDateFormat == dateValue)
        return true;
    else
        return false;
}

function getdate() {
    var now = new Date()
    y = now.getFullYear()
    m = now.getMonth() + 1
    d = now.getUTCDate()
    m = m < 10 ? "0" + m : m
    d = d < 10 ? "0" + d : d
    return d + "/" + m + "/" + y;
}

function CompareFromToDate(dateFromValue, dateToValue) {
    var dateFromSplit = dateFromValue.split("/");
    var fromDate = new Date(dateFromSplit[2], dateFromSplit[1] - 1, dateFromSplit[0]);

    var dateToSplit = dateToValue.split("/");
    var ToDate = new Date(dateToSplit[2], dateToSplit[1] - 1, dateToSplit[0]);

    if (ToDate < fromDate)
        return false;
    else
        return true;
}

function CheckDates(datetimeControlValue) {
    var ret = isDate(datetimeControlValue);
    return ret;
}

function CheckDateOverlapping(dateFrom1, dateTo1, dateFrom2, dateTo2) {
    if (dateFrom1 == "" && dateTo1 == "" || dateFrom2 == "" && dateTo2 == ""
            || dateFrom1 == "" && dateFrom2 == "" || dateTo1 == "" && dateTo2 == "")
        return true;

    if (dateFrom1 != "" && dateTo2 != "" && GetDate(dateFrom1) > GetDate(dateTo2))
        return false;

    if (dateTo1 != "" && dateFrom2 != "" && GetDate(dateTo1) > GetDate(dateFrom2))
        return false;

    //Below is situation that four date are not null
    if (GetDate(dateTo2) < GetDate(dateFrom1) || GetDate(dateFrom2) > GetDate(dateTo1))
        return false;
    else
        return true;
}

function GetDate(dateStr) {
    if (dateStr != '') {
        var arrDate1 = dateStr.split("/");
        var useDate1 = new Date(arrDate1[2], arrDate1[1] - 1, arrDate1[0]).format("yyyy-MM-dd:HH:mm:ss");
        return useDate1;
    }
}
